import React from 'react'
import PropTypes from 'prop-types'
import {Redirect, Route, Switch, routerRedux} from 'dva/router'
import {LocaleProvider} from 'antd'
import zh_CN from 'antd/lib/locale-provider/zh_CN'
import moment from 'moment'
import 'moment/locale/zh-cn';
import dynamic from 'dva/dynamic'
import App from './routes/App'

const {ConnectedRouter} = routerRedux
moment.locale('zh-cn')

function Routers({history, app}) {
  const error = dynamic({
    app,
    component: () => import ('./routes/error')
  })

  const routes = [
    {
      path: '/home',
      models: () => [import ('./models/home')],
      component: () => import ('./routes/home/')
    }
  ]
  return (
    <ConnectedRouter history={history}>
      <LocaleProvider locale={zh_CN}>
        <App>
          <Switch>
            <Route exact path="/" render={() => (<Redirect to="/home"/>)}/> {routes.map(({
              path,
              ...dynamics
            }, key) => (<Route
              key={key}
              exact
              path={path}
              component={dynamic({
              app,
              ...dynamics
            })}/>))
}
            <Route component={error}/>
          </Switch>
        </App>
      </LocaleProvider>
    </ConnectedRouter>
  )
}

Routers.propTypes = {
  history: PropTypes.object,
  app: PropTypes.object
}

export default Routers
